package com.atguigu.daijia.customer.controller;

import com.atguigu.daijia.common.login.Login;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Tag(name = "客户API接口管理")
@RestController
@RequestMapping("/customer")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerController {

    @Resource
    @Qualifier("customerService")
    private CustomerService customerInfoService;

    /*
     * @param code
     * @==============History===============<br/>;
     * @Description //   小程序授权登录
     * @Date 2024/7/31
     * @Author < a href="15991488967@163.com">chen</ a>;
     * @return com.atguigu.daijia.common.result.Result<java.lang.String>
     */
    @Operation(summary = "小程序授权登录")
    @GetMapping("/login/{code}")
    public Result<String> wxLogin(@PathVariable String code) {
        return Result.ok(customerInfoService.login(code));
    }

    /*
     * @param  
     * @==============History===============<br/>;
     * @Description //   获取客户登录信息
     * @Date 2024/8/1
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Login
    @Operation(summary = "获取客户登录信息")
    @GetMapping("/getCustomerLoginInfo")
    public Result<CustomerLoginVo> getCustomerLoginInfo() {
        //1 从ThreadLocal获取用户id
        Long customerId = AuthContextHolder.getUserId();
        //调用service
        CustomerLoginVo customerLoginVo = customerInfoService.getCustomerInfo(customerId);
        return Result.ok(customerLoginVo);
    }

    /*
     * @param null
     * @==============History===============<br/>;
     * @Description //   更新用户微信手机号
     * @Date 2024/8/1
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Login
    @Operation(summary = "更新用户微信手机号")
    @PostMapping("/updateWxPhone")
    public Result updateWxPhone(@RequestBody UpdateWxPhoneForm updateWxPhoneForm) {
        updateWxPhoneForm.setCustomerId(AuthContextHolder.getUserId());
        return Result.ok(customerInfoService.updateWxPhoneNumber(updateWxPhoneForm));
    }
}

